using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace ADiKt {
    public class Game1 : Microsoft.Xna.Framework.Game {
        public static GraphicsDeviceManager graphics;
        ScreenManager screenManager;
        public static SoundEffect crashSound;
        
        //camera detail
        float cameraX = (float) Globals.width / 2f;
        float cameraY = (float) Globals.height / 2f;
        float cameraZoom = 401;
        Matrix viewMatrix;
        Matrix projMatrix;

        /// <summary> sets up the viewport and screen manager </summary>
        public Game1() {
            //shared attributes
            Content.RootDirectory = "Content";
            crashSound = Content.Load<SoundEffect>("crash"); //TODO - move to gameplay screen

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = Globals.width;
            graphics.PreferredBackBufferHeight = Globals.height;

            screenManager = new ScreenManager(this);
            Components.Add(screenManager);
            screenManager.AddScreen(new BackgroundScreen(), null);
            screenManager.AddScreen(new MainMenuScreen(), null);
        }

        /// <summary> scales the viewport </summary>
        protected override void LoadContent() {
            Create2DProjectionMatrix();
            Globals.viewProjMatrix = viewMatrix * projMatrix;
        }

        /// <summary> scales X or Y to balance viewport stretch </summary>
        public void Create2DProjectionMatrix() {
            float projScaleX;
            float projScaleY;
            float width = GraphicsDevice.Viewport.Width;
            float height = GraphicsDevice.Viewport.Height;

            if (width > height) {
                projScaleX = height / width;
                projScaleY = 1.0f;
            } else {
                projScaleX = 1.0f;
                projScaleY = width / height;
            }

            projMatrix = Matrix.CreateScale(projScaleX, projScaleY, 0.0f);
            projMatrix.M43 = 0.5f;
        }

        /// <summary> plases the camera, adds default worms and initializes elements </summary>
        protected override void Initialize() {
            viewMatrix = Matrix.CreateTranslation(-cameraX, -cameraY, 0) * Matrix.CreateScale(1.0f / cameraZoom, 1.0f / cameraZoom, 1.0f);
            base.Initialize();

            new Human(Keys.Left, Keys.Right, Color.Red);
            new Human(Keys.Z, Keys.X, Color.Blue);
            new AI(Color.Cyan);
            new AI(Color.Green);
        }

        /// <summary> draws the seene to the screen </summary>
        protected override void Draw(GameTime gameTime) {
            GraphicsDevice.Clear(Color.Black);
            graphics.GraphicsDevice.Clear(Color.Black);
            base.Draw(gameTime);
        }

    }
}